/*
 * @Author: sdorein sdorein@163.com
 * @Date: 2024-07-26 17:44:26
 * @LastEditors: sdorein sdorein@163.com
 * @LastEditTime: 2024-08-05 14:05:19
 * @FilePath: \vite-as-editor\src\utils\index.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
// utils.ts
// import { v4 as uuidv4 } from 'uuid';
class Utils {
    formatDate(value: string): string {
      const date = new Date(value);
      const y = date.getFullYear();
      const MM = (date.getMonth() + 1).toString().padStart(2, '0');
      const d = date.getDate().toString().padStart(2, '0');
      const h = date.getHours().toString().padStart(2, '0');
      const m = date.getMinutes().toString().padStart(2, '0');
      const s = date.getSeconds().toString().padStart(2, '0');
      return `${y}-${MM}-${d} ${h}:${m}:${s}`;
    }
  
    forEach(obj: object | any[], fn: (value: any, keyOrIndex:  number, collection: object | any[]) => void): void {
      if (obj === null || typeof obj === 'undefined') return;
      if (typeof obj !== 'object') obj = [obj];
      if (Array.isArray(obj)) {
        for (let i = 0, l = obj.length; i < l; i++) {
          fn.call(null, obj[i], i, obj);
        }
      } else {
        for (let key in obj) {
          if (Object.prototype.hasOwnProperty.call(obj, key)) {
            //fn.call(null, obj[key] as any, key, obj);
          }
        }
      }
    }
  
    isArray(val: any): boolean {
      return Object.prototype.toString.call(val) === '[object Array]';
    }
  
    getObjClass(obj: any): string {
      return Object.prototype.toString.call(obj).slice(8, -1);
    }
  
    deepClone(obj: any): any {
      let result: any;
      const objClass = this.getObjClass(obj);
      if (objClass === 'Object') {
        result = {};
      } else if (objClass === 'Array') {
        result = [];
      } else {
        return obj;
      }
      for (let key in obj) {
        const value = obj[key];
        result[key] = this.deepClone(value);
      }
      return result;
    }
  
    assignObj(target: any, sources: any): any {
      let obj = target;
      if (typeof target !== 'object' || typeof sources !== 'object') {
        return sources;
      }
      for (let key in sources) {
        if (target.hasOwnProperty(key)) {
          obj[key] = this.assignObj(target[key], sources[key]);
        } else {
          obj[key] = sources[key];
        }
      }
      return obj;
    }
   getUuid(): string {
    return Math.random().toString(36).substr(2, 9);  
   }
   
  }
  
  export default new Utils();